/**
 * Created by root on 14-12-3.
 */

define(['personalJS/module'], function(app){

  app.controller('ProfileController', ['$scope', '$upload', 'PersonalService', 'ClassHelperService', 'unitList', 'personalInfo',
    function($scope, $upload, PersonalService, ClassHelperService, unitList, personalInfo){
      $scope.unitList = unitList;
      $scope.personalInfo = personalInfo;
      $scope.files = [];  //用于文件（头像）上传

      function checkUnit(){
        if( $scope.personalInfo['unit_id'] ){
          $scope.unit_name = $scope.personalInfo.unit.name;
        }else{
          $scope.unit_name = null;
        }
      }

      checkUnit();

      $scope.$watch('files', function(){
        $scope.files.forEach(function(currentFile){

          $scope.upload = $upload.upload({
            url: PersonalService.getHeadImageUploadUri(),
            method: 'post',
            data: {myObj: $scope.myModelObj},
            file: currentFile
          })
            .progress(function(evt){
              console.log('progress: ' + parseInt(100.0 * evt.loaded / evt.total) + '% file :'+ evt.config.file.name);
            })
            .success(function(data){
              $scope.$emit('message:success', {
                title: '操作成功',
                msg: '头像已更改'
              });

              $scope.$emit('personalInfo:update');
              $scope.personalInfo.head_image = data['head_image'];
            })
            .error(function(data){
              $scope.$emit('message:error', {
                title: '操作失败',
                msg: data.error
              });
            });
        });
      });

      $scope.submitPersonalInfo = function(){
        var updateData = ClassHelperService.clone($scope.personalInfo); console.log(updateData);
        delete updateData['unit'];
        delete updateData['role'];

        PersonalService.updateInfo(updateData)
          .success(function(){
            $scope.$emit('message:success', {
              title: '操作成功',
              msg: '个人信息已修改'
            });

            checkUnit();
          })
          .error(function(data){
            console.error(data);
            $scope.$emit('message:error', {
              title: '操作失败',
              msg: data.error
            })
          })
      };

      $scope.cancel = function(){
        PersonalService.getInfo()
          .success(function(data){
            $scope.personalInfo = data;
            checkUnit();
          })
          .error(function(data){
            console.error(data);
            $scope.$emit('message:error', {
              msg: '刷新用户数据失败'
            });
          });
      };

  }]);

  app.controller('PasswordController', ['$scope', 'PersonalService',
    function($scope, PersonalService){

        //初始化相关变量
        $scope.passwordInfo = {};
        $scope.errorMessages = {};

        /*
         用户更改密码
         */
        $scope.submitPasswordInfo = function() {

          if (PersonalService.checkUpdatePasswordInfo($scope.passwordInfo, $scope.errorMessages)) {

            PersonalService.updatePassword($scope.passwordInfo)
              .success(function () {
                alert('密码已经成功修改');
                $scope.errorMessages = {};
              })

              .error(function (data) {
                $scope.errorMessages = data.errorMessages;
              });
          }
        };
  }]);

});